<template>
	<view class="ljapp change">
		<view class="dev">
			<view class="content">
				<view class="card">
					<u-radio-group
						placement="column"
						iconPlacement="right"
						borderBottom
						
						size="44rpx"
						activeColor="#0daef1"
						v-model="radioValue"
					>
						<u-radio
							v-for="(item,index) in radioList" :key="index"
							:label="item.label"
							:name="item.name">
						</u-radio>
					</u-radio-group>
				</view>
				
				<view class="btn-fillet btn-submit" @click="changeRole" v-if="userInfo.id">确认切换</view>
				<view class="btn-fillet btn-submit" style="background-color: #aaa;box-shadow: 0 3px 3px #ccc;" v-else>确认切换</view>
			</view>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				radioList: [
					{
						name: 'patient',
						label: '患者端',
						url: '/pages/home/index'
					},
					{
						name: 'doctor',
						label: '医生端',
						url: '/pages/doctor/index',
						url2: '/pagesD/doctor/apply'
					},
					{
						name: 'pharmacist',
						label: '药师端',
						url: '/pagesA/prescript/index',
						url2: '/pagesP/pharmacist/apply'
					},
					{
						name: 'business',
						label: '机构端',
						url: '/pagesB/business/index',
						url2: '/pagesB/business/apply'
					},
				],
				radioValue: "patient",
				
				userInfo: {}, //用户信息
			}
		},
		onShow() {
			this.radioValue = this.$store.state.role
			this.Oninfo()
		},
		methods: {
			Oninfo() {
				let that = this
				
				this.$Http({
					url: 'getUserInfo',
					type: 'POST',
					data: {}
				}).then(res => {
					if (res.code == 1) {
						this.userInfo = res.data
					}
				})
			},
			changeRole(){
				var url = "", roleInfo = {}, isReLaunch = false
				
				switch(this.radioValue){
					case 'patient':
						break;
					case 'doctor':
						roleInfo = this.userInfo.doctor_info || {}
						break;
					case 'pharmacist':
						roleInfo = this.userInfo.pharmacist_info || {}
						break;
					case 'business':
						roleInfo = this.userInfo.business_info || {}
						break;
				}
				if(roleInfo&&roleInfo.auditstatus==1&&roleInfo.status=="hidden"){
					return this.$u.toast('账号异常，无法登录，请联系管理人员');
				}
				
				for(var i in this.radioList){
					if(this.radioList[i].name == this.radioValue){
						if(this.userInfo.id && (this.userInfo['is_'+this.radioValue]==undefined || this.userInfo['is_'+this.radioValue]==1)){
							url = this.radioList[i].url
							isReLaunch = true
						}else{
							url = this.radioList[i].url2
						}
						break;
					}
				}
				
				if(url){
					this.$store.commit('changeRole', this.radioValue)
					if(this.radioValue=='patient'){
						uni.reLaunch({
							url: url
						})
					}else{
						console.log(url)
						if(isReLaunch){
							uni.reLaunch({
								url: url
							})
						}else{
							uni.navigateTo({
								url: url
							})
						}
					}
				}
			}
		}
	}
</script>

<style lang="less">
	@import '@/pages/style.less';
</style>
